package doublelinklistsentinel;

public class DoubleLinkListSentinel {
    public static class  Node {
        Node prev;
        int element;
        Node next;

        public Node(Node prev, int element, Node next) {
            this.prev = prev;
            this.element = element;
            this.next = next;
        }
    }

    Node sentinel = new Node(null,-1,null);

    public DoubleLinkListSentinel() {
        sentinel.next = sentinel;
        sentinel.prev = sentinel;
    }
    public void addFirst(int element) {
        if (sentinel.next == sentinel) {
            sentinel.next = new Node(sentinel, element, sentinel);
        }
        Node last = sentinel.prev;
        last.next = new Node(last, element, sentinel);
        sentinel.prev = last.next;
    }

    public void out() {
        recursion(sentinel.next);
    }
    //resursion
    private Node recursion(Node point) {
        if (point.next == sentinel) {
            return null;
        }
        System.out.println(point.element);
        return recursion(point.next);
    }
}
